package com.ww.springboot.boot.thread.threadlocal;

/**
 * 描述：InheritableThreadLocal原理
 * 在创建线程时 会复制父线程的InheritableThreadLocal值
 * 但是线程池中的线程只会创建一次 如果另外的父线程想传递值给子线程 那么子线程无法获取
 *
 * @author 🧑 ‍wanwei
 * @since 2022-06-10 15:09
 */
public class 父子线程传值Demo1 {

    private static final ThreadLocal threadLocal = new ThreadLocal();
    private static final InheritableThreadLocal threadLocal2 = new InheritableThreadLocal();

    public static void main(String[] args) {
        threadLocal.set("a");
        threadLocal2.set("aa");

        new Thread(() -> {
            //threadLocal.set("b");
            //threadLocal2.set("bb");
            System.out.println("子1:" + threadLocal.get());
            System.out.println("子1:" + threadLocal2.get());
            new Thread(() -> {
                System.out.println("子2:" + threadLocal.get());
                System.out.println("子2:" + threadLocal2.get());
            }).start();
        }).start();

    }
}
